﻿using log4net;
using System.Reflection;
using Koala.Pro.CodeGenerator.UI.DatabaseModel.Schema;
using Koala.Pro.CodeGenerator.UI.Controls;

namespace Koala.Pro.CodeGenerator.UI.Common
{
    public class SchemaBuilder
    {
        private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        public static DbInstanceInfo CreateDatabaseSchema(DbInstanceInfo dbInstance, IProgressCallback progress)
        {
            return CreateDatabaseSchema(dbInstance, progress, null);
        }

        public static DbInstanceInfo CreateDatabaseSchema(DbInstanceInfo dbInstance, IProgressCallback progress, string databaseName)
        {
            MainSetting mainSetting = new MainSetting();
            mainSetting.Load();
            dbInstance = new DbInstanceInfo(mainSetting.DatabaseType, mainSetting.NoPrefixOfTableName);
            int stepto = 0;
            int max = 2;
            string @string = "准备加载数据库信息......";
            if (!ProgressBarHelper.StartProgress(progress, @string, max, ref stepto))
            {
                return null;
            }
            @string = "正在加载数据库信息......";
            if (!ProgressBarHelper.IncrProgress(progress, @string, ref stepto))
            {
                return null;
            }
            IDictionary<string, DatabaseInfo> dictionary = dbInstance.ListDatabases();
            @string = "加载数据库信息完成";
            stepto = 1;
            if (!ProgressBarHelper.IncrProgress(progress, @string, ref stepto))
            {
                return null;
            }
            if (!string.IsNullOrEmpty(databaseName))
            {
                stepto = 0;
                max = 2;
                if (!ProgressBarHelper.StartProgress(progress, "正在加载数据库信息......", max, ref stepto))
                {
                    return null;
                }
                DatabaseInfo databaseInfo = dictionary[databaseName];
                if (databaseInfo != null)
                {
                    databaseInfo.ColumnAliasHandler = GetColumnAlias;
                    databaseInfo.TableAliasHandler = GetTableAlias;
                    string string2 = "正在加载表信息......";
                    if (!ProgressBarHelper.IncrProgress(progress, string2, ref stepto))
                    {
                        return null;
                    }
                    databaseInfo.ListTables();
                    string2 = "正在加载视图信息......";
                    if (!ProgressBarHelper.IncrProgress(progress, string2, ref stepto))
                    {
                        return null;
                    }
                    databaseInfo.ListViews();
                    string2 = "正在加载存储过程信息......";
                    if (!ProgressBarHelper.IncrProgress(progress, string2, ref stepto))
                    {
                        return null;
                    }
                    databaseInfo.ListProcedures();
                }
            }
            stepto = 2;
            ProgressBarHelper.IncrProgress(progress, "加载完成", ref stepto);
            return dbInstance;
        }

        public static string GetColumnAlias(string tableName, string columnName, string databaseType)
        {
            string result = string.Empty;
            try
            {
                ColumnNameAliasInfo columnNameAliasInfo = ColumnNameAliasInfo.GetAliasByName(tableName, columnName, databaseType);
                if (columnNameAliasInfo != null && !string.IsNullOrEmpty(columnNameAliasInfo.ColumnAlias))
                {
                    result = columnNameAliasInfo.ColumnAlias;
                }
            }
            catch (Exception exception)
            {
                log.Error("Error when invokes GetAliasName", exception);
                throw;
            }
            return result;
        }

        public static string GetTableAlias(string tableName, string databaseType)
        {
            string result = string.Empty;
            try
            {
                TableNameAliasInfo tableNameAliasInfo = TableNameAliasInfo.GetAliasByName(tableName, databaseType);
                if (tableNameAliasInfo != null && !string.IsNullOrEmpty(tableNameAliasInfo.TableAlias))
                {
                    result = tableNameAliasInfo.TableAlias;
                }
            }
            catch (Exception exception)
            {
                log.Error("Error when invokes GetAliasName", exception);
                throw;
            }
            return result;
        }
    }

}
